package com.memorywords.controller;

import com.memorywords.pojo.PageResult;
import com.memorywords.pojo.Result;
import com.memorywords.service.LikeService;
import com.memorywords.utils.ThreadLocalUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Slf4j
@Validated
@RestController
@RequestMapping("/like")
public class LikeController {
    @Autowired
    private LikeService likeService;

    @GetMapping("/check")
    public Result<Boolean> check(@RequestHeader("Authorization") String token, int wordId){
        Map<String, Object> map = ThreadLocalUtil.get();
        int userId = (int) map.get("userId");
        if(likeService.check(userId, wordId)){
            return Result.success(true);
        }else{
            return Result.success(false);
        }
    }

    @PostMapping("change")
    public Result change(@RequestHeader("Authorization") String token, int wordId, int status){
        Map<String, Object> map = ThreadLocalUtil.get();
        int userId = (int) map.get("userId");
        likeService.change(userId, wordId, status);
        return Result.success();
    }

    @GetMapping("/getList")
    public Result<PageResult> getList(@RequestHeader("Authorization") String token, int pageNum, int pageSize){
        Map<String, Object> map = ThreadLocalUtil.get();
        int userId = (int) map.get("userId");
        PageResult list = likeService.getList(userId, pageNum, pageSize);
        return Result.success(list);
    }


}
